<?php


class EmsContentTable extends Doctrine_Table
{
    
    public static function getInstance()
    {
        return Doctrine_Core::getTable('EmsContent');
    }

    public function addContentByTitleLikeQuery($string, $limit, Doctrine_Query $q = null)
    {
        if (is_null($q))
        {
            $q = Doctrine_Query::create()
                ->from('EmsContent c');
        }

        $alias = $q->getRootAlias();

        $q->andWhere('LOWER('.$alias.'.title) LIKE LOWER(?)', '%'.$string.'%')
                ->addOrderBy($alias.'.title')->limit($limit);

        return $q;
    }

    public function addEnabledContentsQuery(Doctrine_Query $q = null)
    {
        if (is_null($q))
        {
            $q = Doctrine_Query::create()
                ->from('EmsContent c');
        }

        $alias = $q->getRootAlias();

        $q->andWhere($alias . '.is_enabled = ?', 1);

        return $q;
    }

    public function addPublishedContentsQuery(Doctrine_Query $q = null)
    {
        if (is_null($q))
        {
            $q = Doctrine_Query::create()
                ->from('EmsContent c');
        }

        $alias = $q->getRootAlias();

        $q->andWhere($alias . '.published = ?', 1);

        return $q;
    }

    public function addFilterByTypeQuery($type_id, Doctrine_Query $q = null)
    {
        if (is_null($q))
        {
            $q = Doctrine_Query::create()
                ->from('EmsContent c');
        }

        $alias = $q->getRootAlias();
        
        $q->andWhere($alias . '.type_id = ?', $type_id);

        return $q;
    }

    public function addPortalContentQuery($portal_id, Doctrine_Query $q = null)
    {
        if (is_null($q))
        {
            $q = Doctrine_Query::create()
                ->from('EmsContent c');
        }

        $alias = $q->getRootAlias();

        $q->leftJoin($alias.'.Section s')
                ->andWhere('s.portal_id = ?', $portal_id)
                ->addOrderBy($alias.'.type_id');

        return $q;
    }

    public function addContentsBySectionIdQuery($section_id, Doctrine_Query $q = null)
    {
        if (is_null($q))
        {
            $q = Doctrine_Query::create()
                ->from('EmsContent c');
        }

        $alias = $q->getRootAlias();

        $q->andWhere($alias . '.section_id = ?', $section_id);

        return $q;
    }

    public function getPortalEnabledAndPublishedContentsFilteredByTypeByTitleLike($string, $limit, $portal_id, $type_id)
    {
        return $this->addContentByTitleLikeQuery($string, $limit,
                    $this->addFilterByTypeQuery($type_id,
                        $this->addEnabledContentsQuery(
                                $this->addPublishedContentsQuery(
                                    $this->addPortalContentQuery($portal_id)))))
                        ->execute();
    }

    public function getEnabledAndPublishedContentsBySectionIdQuery($section_id)
    {
        return $this->addContentsBySectionIdQuery($section_id,
                    $this->addPublishedContentsQuery(
                        $this->addEnabledContentsQuery()));
    }

    public function getEnabledAndPublishedContentsBySectionId($section_id)
    {
        return $this->getEnabledAndPublishedContentsBySectionIdQuery($section_id)->execute();
    }
}